#!/bin/bash

# 定义输出文件名（使用主机名+时间戳格式）
HOSTNAME=$(hostname | tr -d ' ')  # 移除主机名中的空格（如果有）
OUTPUT_FILE="【${HOSTNAME}】-system_info_$(date +%Y%m%d_%H%M%S).html"
{
# 获取操作系统信息的改进方法
OS_INFO=""
if [ -f /etc/os-release ]; then
    # 大多数现代Linux发行版使用这个文件
    OS_INFO=$(grep PRETTY_NAME /etc/os-release | cut -d'"' -f2)
elif [ -f /etc/redhat-release ]; then
    # 旧版RedHat/CentOS
    OS_INFO=$(cat /etc/redhat-release)
elif [ -f /etc/lsb-release ]; then
    # 某些Debian/Ubuntu系统
    OS_INFO=$(grep DISTRIB_DESCRIPTION /etc/lsb-release | cut -d'"' -f2)
elif command -v lsb_release &> /dev/null; then
    # 如果上述文件都不存在，但lsb_release命令可用
    OS_INFO=$(lsb_release -d | cut -f2-)
else
    # 最后的手段
    OS_INFO="$(uname -s) $(uname -r)"
fi

# 获取系统运行时间
UPTIME=$(uptime -p | sed 's/up //')

# 获取CPU信息
CPU_MODEL=$(grep "model name" /proc/cpuinfo | head -n1 | cut -d ":" -f2 | sed 's/^[ \t]*//')
CPU_CORES=$(grep -c "processor" /proc/cpuinfo)
CPU_SOCKETS=$(grep "physical id" /proc/cpuinfo | sort | uniq | wc -l)
CPU_CORES_PER_SOCKET=$(grep "cpu cores" /proc/cpuinfo | head -n1 | cut -d: -f2 | tr -d ' ')
CPU_THREADS_PER_CORE=$((CPU_CORES / (CPU_SOCKETS * CPU_CORES_PER_SOCKET)))
CPU_THREADS=$((CPU_CORES * CPU_THREADS_PER_CORE))

# 获取CPU使用率信息
CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}')
CPU_USER=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d'u' -f1)
CPU_SYSTEM=$(top -bn1 | grep "Cpu(s)" | awk '{print $4}' | cut -d's' -f1)
CPU_IOWAIT=$(top -bn1 | grep "Cpu(s)" | awk '{print $10}' | cut -d'w' -f1)
CPU_STEAL=$(top -bn1 | grep "Cpu(s)" | awk '{print $16}' | cut -d's' -f1)

# 获取CPU频率信息
CPU_MIN_FREQ=$(cat /proc/cpuinfo | grep "cpu MHz" | sort -n | head -1 | cut -d: -f2 | tr -d ' ')
CPU_MAX_FREQ=$(cat /proc/cpuinfo | grep "cpu MHz" | sort -n | tail -1 | cut -d: -f2 | tr -d ' ')
CPU_CUR_FREQ=$(cat /proc/cpuinfo | grep "cpu MHz" | head -1 | cut -d: -f2 | tr -d ' ')

# 获取CPU负载信息
LOAD_AVG=$(cat /proc/loadavg | awk '{print $1", "$2", "$3}')
UPTIME_LOAD=$(uptime | awk -F'average:' '{print $2}' | sed 's/^[ \t]*//')

# 开始生成HTML文件
cat > $OUTPUT_FILE <<EOF
<!DOCTYPE html>
<html>
<head>
    <title>系统信息报告</title>
    <meta charset="UTF-8">
    <style>
        body { 
            font-family: Arial, Helvetica, sans-serif; 
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
            color: #333;
            line-height: 1.6;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: #fff;
            padding: 30px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
            border-radius: 5px;
        }
        .header {
            text-align: center;
            margin-bottom: 30px;
            padding-bottom: 20px;
            border-bottom: 1px solid #e0e0e0;
        }
        h1 { 
            color: #2c3e50; 
            margin: 0;
            font-size: 28px;
        }
        .subtitle {
            color: #666;
            font-size: 16px;
            margin-top: 5px;
        }
        .author {
            color: #777;
            font-style: italic;
            margin-top: 10px;
            font-size: 14px;
        }
        .timestamp {
            color: #666;
            font-size: 14px;
            text-align: center;
            margin-bottom: 20px;
        }
        h2 { 
            color: #2980b9; 
            margin-top: 30px; 
            padding-bottom: 8px;
            border-bottom: 2px solid #2980b9;
            font-size: 20px;
        }
        table { 
            border-collapse: collapse; 
            width: 100%; 
            margin-bottom: 20px;
        }
        th { 
            background-color: #2980b9; 
            color: white; 
            padding: 10px;
            text-align: left;
            font-weight: normal;
        }
        td { 
            padding: 8px 10px;
            border-bottom: 1px solid #e0e0e0;
        }
        tr:nth-child(even) {
            background-color: #f8f8f8;
        }
        tr:hover {
            background-color: #f0f7fc;
        }
        pre { 
            background-color: #f8f8f8; 
            padding: 12px; 
            border-radius: 3px; 
            overflow-x: auto;
            border-left: 3px solid #2980b9;
            font-family: Consolas, Monaco, monospace;
            font-size: 14px;
            line-height: 1.4;
        }
        .net-details { 
            margin: 15px 0; 
            display: none;
        }
        .net-title { 
            font-weight: bold; 
            margin: 12px 0 8px;
            color: #2c3e50;
            font-size: 16px;
        }
        .toggle-btn { 
            background-color: #2980b9; 
            color: white; 
            border: none; 
            padding: 6px 12px; 
            text-align: center; 
            text-decoration: none; 
            display: inline-block; 
            font-size: 13px; 
            margin: 2px; 
            cursor: pointer; 
            border-radius: 3px;
        }
        .toggle-btn:hover {
            background-color: #2472a4;
        }
        .net-card { 
            margin-bottom: 15px; 
            padding: 15px; 
            border: 1px solid #e0e0e0; 
            border-radius: 4px;
            background-color: #fff;
        }
        .summary-table { 
            width: auto;
            margin: 0 auto;
        }
        .highlight {
            color: #c0392b;
            font-weight: bold;
        }
        .compact-info {
            display: flex;
            flex-wrap: wrap;
            gap: 20px;
            margin-bottom: 15px;
        }
        .compact-item {
            flex: 1;
            min-width: 200px;
        }
        .compact-label {
            font-weight: bold;
            color: #666;
            margin-bottom: 5px;
        }
        .compact-value {
            font-size: 16px;
        }
        .cpu-usage-bar {
            height: 20px;
            background-color: #e0e0e0;
            border-radius: 3px;
            margin-top: 5px;
            overflow: hidden;
        }
        .cpu-usage-fill {
            height: 100%;
            background-color: #2980b9;
            display: flex;
            align-items: center;
            justify-content: flex-end;  /* 改为右对齐 */
            color: white;
            font-size: 12px;
            font-weight: bold;
            padding-right: 5px;  /* 添加右侧内边距 */
            box-sizing: border-box;  /* 确保内边距不影响宽度计算 */
            min-width: 40px;  /* 设置最小宽度确保文本完整显示 */
        }
    </style>
    <script>
        function toggleDetails(iface) {
            var elem = document.getElementById(iface + '-details');
            if (elem.style.display === 'none') {
                elem.style.display = 'block';
            } else {
                elem.style.display = 'none';
            }
        }
    </script>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>系统信息报告</h1>
            <div class="subtitle">全面的系统配置与状态分析</div>
            <div class="timestamp">报告生成时间: $(date "+%Y-%m-%d %H:%M:%S")</div>
            <div class="author">作者: luzz</div>
        </div>

        <h2>系统基本信息</h2>
        <table>
            <tr><th>项目</th><th>值</th></tr>
            <tr><td>主机名</td><td>$(hostname)</td></tr>
            <tr><td>操作系统</td><td>$OS_INFO</td></tr>
            <tr><td>内核版本</td><td>$(uname -r)</td></tr>
            <tr><td>系统架构</td><td>$(uname -m)</td></tr>
            <tr><td>系统运行时间</td><td class="highlight">$UPTIME</td></tr>
        </table>

        <h2>CPU 信息</h2>
        <div class="compact-info">
            <div class="compact-item">
                <div class="compact-label">CPU 型号</div>
                <div class="compact-value">$CPU_MODEL</div>
            </div>
            <div class="compact-item">
                <div class="compact-label">物理 CPU 数量</div>
                <div class="compact-value">$CPU_SOCKETS</div>
            </div>
            <div class="compact-item">
                <div class="compact-label">每CPU核心数</div>
                <div class="compact-value">$CPU_CORES_PER_SOCKET</div>
            </div>
            <div class="compact-item">
                <div class="compact-label">总核心数</div>
                <div class="compact-value">$CPU_CORES</div>
            </div>
            <div class="compact-item">
                <div class="compact-label">每核线程数</div>
                <div class="compact-value">$CPU_THREADS_PER_CORE</div>
            </div>
            <div class="compact-item">
                <div class="compact-label">总线程数</div>
                <div class="compact-value">$CPU_THREADS</div>
            </div>
        </div>

        <h2>CPU 使用情况</h2>
        <table>
            <tr><th>项目</th><th>值</th></tr>
            <tr><td>总使用率</td>
                <td>
                    $CPU_USAGE
                    <div class="cpu-usage-bar">
                        <div class="cpu-usage-fill" style="width: ${CPU_USAGE%%%}%">$CPU_USAGE</div>
                    </div>
                </td>
            </tr>
            <tr><td>用户空间使用率</td><td>$CPU_USER%</td></tr>
            <tr><td>系统空间使用率</td><td>$CPU_SYSTEM%</td></tr>
            <tr><td>I/O 等待</td><td>$CPU_IOWAIT%</td></tr>
            <tr><td>Steal 时间</td><td>$CPU_STEAL%</td></tr>
            <tr><td>系统负载 (1, 5, 15分钟)</td><td>$LOAD_AVG</td></tr>
            <tr><td>运行时间负载</td><td>$UPTIME_LOAD</td></tr>
            <tr><td>当前频率</td><td>$CPU_CUR_FREQ MHz</td></tr>
            <tr><td>最低频率</td><td>$CPU_MIN_FREQ MHz</td></tr>
            <tr><td>最高频率</td><td>$CPU_MAX_FREQ MHz</td></tr>
        </table>

        <h2>硬件信息</h2>
        <table>
            <tr><th>项目</th><th>值</th></tr>
            <tr><td>服务器型号</td><td>$(sudo dmidecode -s system-product-name)</td></tr>
            <tr><td>系统厂商</td><td>$(sudo dmidecode -s system-manufacturer)</td></tr>
            <tr><td>内存型号</td><td>$(sudo dmidecode -t memory | grep "Part Number" | head -n1 | cut -d":" -f2 | sed 's/^[ \t]*//;s/[ \t]*$//')</td></tr>
            <tr><td>内存卡槽数量</td><td>$(sudo dmidecode -t memory | grep "Memory Device" | wc -l)</td></tr>
            <tr><td>已安装内存数量</td><td>$(sudo dmidecode -t memory | grep "Size: " | grep -v "No Module Installed" | wc -l)</td></tr>
            <tr><td>内存支持类型</td><td>$(sudo dmidecode -t memory | grep "Type: " | head -n1 | cut -d":" -f2 | sed 's/^[ \t]*//;s/[ \t]*$//')</td></tr>
            <tr><td>内存频率</td><td>$(sudo dmidecode -t memory | grep "Speed: " | head -n1 | cut -d":" -f2 | sed 's/^[ \t]*//;s/[ \t]*$//')</td></tr>
            <tr><td>内存大小</td><td class="highlight">$(free -h | grep "Mem:" | awk '{print $2}')</td></tr>
        </table>

        <h2>内存使用情况</h2>
        <pre>$(free -h)</pre>

        <h2>磁盘使用情况</h2>
        <pre>$(df -TH)</pre>

        <h2>块设备信息</h2>
        <pre>$(lsblk)</pre>

        <h2>网络信息速览</h2>
        <table class="summary-table">
            <tr><th>网卡名</th><th>IP地址</th><th>MAC地址</th><th>操作</th></tr>
EOF

# 获取网络接口列表
INTERFACES=$(ip link show | grep -E '^[0-9]+:' | awk -F: '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//' | grep -v "lo")

for iface in $INTERFACES; do
    # 获取基本网络信息
    IP_ADDR=$(ip addr show $iface | grep -E 'inet[^6]' | awk '{print $2}' | cut -d'/' -f1 | tr '\n' ' ')
    MAC_ADDR=$(ip link show $iface | grep "link/ether" | awk '{print $2}')
    
    # 输出速览表格
    echo "            <tr><td>$iface</td><td>$IP_ADDR</td><td>$MAC_ADDR</td><td><button class=\"toggle-btn\" onclick=\"toggleDetails('$iface')\">查看详情</button></td></tr>" >> $OUTPUT_FILE
done

# 继续HTML文件
cat >> $OUTPUT_FILE <<EOF
        </table>

        <h2>网络详细信息</h2>
        <p>点击上方按钮查看对应网卡的详细信息</p>
EOF

# 生成详细的网卡信息
for iface in $INTERFACES; do
    # 获取详细网络信息
    IP_ADDR=$(ip addr show $iface | grep -E 'inet[^6]' | awk '{print $2}' | cut -d'/' -f1 | tr '\n' ' ')
    MAC_ADDR=$(ip link show $iface | grep "link/ether" | awk '{print $2}')
    SPEED=$(cat /sys/class/net/$iface/speed 2>/dev/null || echo "未知")
    DUPLEX=$(cat /sys/class/net/$iface/duplex 2>/dev/null || echo "未知")
    DRIVER=$(ethtool -i $iface 2>/dev/null | grep driver | awk '{print $2}')
    PCI_INFO=$(lspci | grep $(basename $(readlink /sys/class/net/$iface/device)) | head -n1 | cut -d: -f2-)
    MTU=$(ip link show $iface | grep mtu | awk '{print $5}')
    RX_PACKETS=$(cat /sys/class/net/$iface/statistics/rx_packets)
    TX_PACKETS=$(cat /sys/class/net/$iface/statistics/tx_packets)
    RX_BYTES=$(cat /sys/class/net/$iface/statistics/rx_bytes)
    TX_BYTES=$(cat /sys/class/net/$iface/statistics/tx_bytes)
    ETHTOOL_INFO=$(sudo ethtool $iface 2>/dev/null)
    QUEUES=$(ls -1 /sys/class/net/$iface/queues/ 2>/dev/null | wc -l)

    # 输出详细网卡信息
    cat >> $OUTPUT_FILE <<EOF
        <div id="$iface-details" class="net-details">
            <div class="net-card">
                <h3>网络接口: $iface</h3>
                <table>
                    <tr><th>属性</th><th>值</th></tr>
                    <tr><td>IP地址</td><td class="highlight">$IP_ADDR</td></tr>
                    <tr><td>MAC地址</td><td>$MAC_ADDR</td></tr>
                    <tr><td>速度</td><td>$SPEED Mbps</td></tr>
                    <tr><td>双工模式</td><td>$DUPLEX</td></tr>
                    <tr><td>驱动</td><td>$DRIVER</td></tr>
                    <tr><td>PCI信息</td><td>$PCI_INFO</td></tr>
                    <tr><td>MTU</td><td>$MTU</td></tr>
                </table>
                
                <div class="net-title">流量统计:</div>
                <table>
                    <tr><th>类型</th><th>数据包</th><th>字节数</th></tr>
                    <tr><td>接收</td><td>$RX_PACKETS</td><td>$(echo $RX_BYTES | awk '{printf "%.2f MB", $1/1024/1024}')</td></tr>
                    <tr><td>发送</td><td>$TX_PACKETS</td><td>$(echo $TX_BYTES | awk '{printf "%.2f MB", $1/1024/1024}')</td></tr>
                </table>
EOF

    # 输出ethtool信息
    if [ -n "$ETHTOOL_INFO" ]; then
        cat >> $OUTPUT_FILE <<EOF
                <div class="net-title">Ethtool 信息:</div>
                <pre>$ETHTOOL_INFO</pre>
EOF
    fi

    # 输出队列信息
    if [ $QUEUES -gt 0 ]; then
        cat >> $OUTPUT_FILE <<EOF
                <div class="net-title">队列信息:</div>
                <table>
                    <tr><th>队列类型</th><th>数量</th></tr>
                    <tr><td>RX队列</td><td>$(ls -1 /sys/class/net/$iface/queues/ | grep rx | wc -l)</td></tr>
                    <tr><td>TX队列</td><td>$(ls -1 /sys/class/net/$iface/queues/ | grep tx | wc -l)</td></tr>
                </table>
EOF
    fi

    cat >> $OUTPUT_FILE <<EOF
            </div>
        </div>
EOF
done

# 结束HTML文件
cat >> $OUTPUT_FILE <<EOF
    </div>
</body>
</html>
EOF

# 此代码快内容静默输出
} > /dev/null 2>&1
echo "系统信息报告已生成: $OUTPUT_FILE"